package com.glasses.business.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.glasses.business.R;
import com.glasses.business.appliction.MyApplication;
import com.glasses.business.base.AppBaseFragment;
import com.glasses.business.entity.CheckMenu;
import com.glasses.business.event.BussEvent;

import net.neiquan.applibrary.numberpicker.view.AlertPickerSingle;

import org.haitao.common.utils.AppLog;
import org.haitao.common.utils.ToastUtil;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * Author：邓勇军
 * English Name：Deng,The Sky Fucker!
 * Email：dandycoder@126.com
 * Date：2017/3/8
 * Version：1.0
 */

public class HandLightFragment extends AppBaseFragment {
    @InjectView(R.id.leftEyeTv)
    TextView leftEyeTv;
    @InjectView(R.id.leftEyeRv)
    RelativeLayout leftEyeRv;
    @InjectView(R.id.leftSgTv)
    TextView leftSgTv;
    @InjectView(R.id.leftSgRv)
    RelativeLayout leftSgRv;
    @InjectView(R.id.rightEyeTv)
    TextView rightEyeTv;
    @InjectView(R.id.rightEyeRv)
    RelativeLayout rightEyeRv;
    @InjectView(R.id.rightSgTv)
    TextView rightSgTv;
    @InjectView(R.id.rightSgRv)
    RelativeLayout rightSgRv;
    @InjectView(R.id.tjTv)
    TextView tjTv;
    @InjectView(R.id.tjRv)
    RelativeLayout tjRv;
    @InjectView(R.id.sureTv)
    TextView sureTv;
    @InjectView(R.id.leftEyeImg)
    ImageView leftEyeImg;
    @InjectView(R.id.leftSgImg)
    ImageView leftSgImg;
    @InjectView(R.id.rightEyeImg)
    ImageView rightEyeImg;
    @InjectView(R.id.rightSgImg)
    ImageView rightSgImg;
    @InjectView(R.id.tjImg)
    ImageView tjImg;
    @InjectView(R.id.threeMonthImg)
    ImageView threeMonthImg;
    @InjectView(R.id.threeMonthLL)
    LinearLayout threeMonthLL;
    @InjectView(R.id.sixMonthImg)
    ImageView sixMonthImg;
    @InjectView(R.id.sixMonthLL)
    LinearLayout sixMonthLL;
    @InjectView(R.id.nineMonthImg)
    ImageView nineMonthImg;
    @InjectView(R.id.nineMonthLL)
    LinearLayout nineMonthLL;
    @InjectView(R.id.leftAxlEv)
    TextView leftAxlEv;
    @InjectView(R.id.rightAxlEv)
    TextView rightAxlEv;
    private int monthFlag = 0;

    @Override
    public int getRootViewId() {
        return R.layout.fragment_write_light;
    }

    @Override
    public void setViews() {
        setTitleTv("新建验光单");
    }

    @Override
    public void initData() {
        ScrollView scrollView;
    }

    @Override
    public boolean mainTabShow() {
        return false;
    }


    @OnClick({R.id.leftAxlEv, R.id.rightAxlEv, R.id.threeMonthLL, R.id.sixMonthLL, R.id.nineMonthLL, R.id.leftEyeRv, R.id.leftSgRv, R.id.rightEyeRv, R.id.rightSgRv, R.id.tjRv, R.id.sureTv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.leftAxlEv:
                ArrayList<String> axl = new ArrayList<>();
                for (int i = 0; i < 185; i = i + 5) {
                    axl.add(i + "");
                }
                new AlertPickerSingle.Builder(getActivity())
                        .setData(axl)
                        .setOnItemSelectListener(new AlertPickerSingle.OnItemSelectListener() {
                            @Override
                            public void endSelect(AlertPickerSingle chooser, boolean isCancel, int id, String text) {
                                leftAxlEv.setText(text);
                            }
                        })
                        .show();
                break;
            case R.id.rightAxlEv:
                ArrayList<String> axl_ = new ArrayList<>();
                for (int i = 0; i < 185; i = i + 5) {
                    axl_.add(i + "");
                }
                new AlertPickerSingle.Builder(getActivity())
                        .setData(axl_)
                        .setOnItemSelectListener(new AlertPickerSingle.OnItemSelectListener() {
                            @Override
                            public void endSelect(AlertPickerSingle chooser, boolean isCancel, int id, String text) {
                                rightAxlEv.setText(text);
                            }
                        })
                        .show();
                break;
            case R.id.leftEyeRv:
                ArrayList<String> sph = new ArrayList<>();
                for (double i = -20.00; i < -10.00; i = i + 0.5) {
                    sph.add(i + "");
                }
                for (double i = -10.00; i < 10.00; i = i + 0.25) {
                    if(i>0) {
                        sph.add("+"+i + "");
                    }else{
                        sph.add(i + "");
                    }
                }
                for (double i = 10.00; i <= 20.00; i = i + 0.5) {
                    sph.add("+"+i + "");
                }
                new AlertPickerSingle.Builder(getActivity())
                        .setData(sph)
                        .setDefaultIndex(60)
                        .setOnItemSelectListener(new AlertPickerSingle.OnItemSelectListener() {
                            @Override
                            public void endSelect(AlertPickerSingle chooser, boolean isCancel, int id, String text) {
                                leftEyeImg.setVisibility(View.GONE);
                                leftEyeTv.setText(text);
                            }
                        })
                        .show();
                break;
            case R.id.rightEyeRv:
                ArrayList<String> sph_ = new ArrayList<>();
                for (double i = -20.00; i < -10.00; i = i + 0.5) {
                    sph_.add(i + "");
                }
                for (double i = -10.00; i < 10.00; i = i + 0.25) {
                    if(i>0) {
                        sph_.add("+"+i + "");
                    }else{
                        sph_.add(i + "");
                    }
                }
                for (double i = 10.00; i <= 20.00; i = i + 0.5) {
                    sph_.add("+"+i + "");
                }
                new AlertPickerSingle.Builder(getActivity())
                        .setData(sph_)
                        .setDefaultIndex(60)
                        .setOnItemSelectListener(new AlertPickerSingle.OnItemSelectListener() {
                            @Override
                            public void endSelect(AlertPickerSingle chooser, boolean isCancel, int id, String text) {
                                rightEyeImg.setVisibility(View.GONE);
                                rightEyeTv.setText(text);
                            }
                        })
                        .show();
                break;
            case R.id.leftSgRv:
                ArrayList<String> cyl = new ArrayList<>();
                for (double i = -2.00; i < 2.25; i = i + 0.25) {
                    if(i<=0){
                        cyl.add(i + "");
                    }else {
                        cyl.add("+"+i + "");
                    }
                }
                new AlertPickerSingle.Builder(getActivity())
                        .setData(cyl)
                        .setDefaultIndex(8)
                        .setOnItemSelectListener(new AlertPickerSingle.OnItemSelectListener() {
                            @Override
                            public void endSelect(AlertPickerSingle chooser, boolean isCancel, int id, String text) {
                                leftSgImg.setVisibility(View.GONE);
                                leftSgTv.setText(text);
                            }
                        })
                        .show();
                break;
            case R.id.rightSgRv:
                ArrayList<String> cyl_ = new ArrayList<>();
                for (double i = -2.00; i < 2.25; i = i + 0.25) {
                    if(i<=0){
                        cyl_.add(i + "");
                    }else {
                        cyl_.add("+"+i + "");
                    }
                }
                new AlertPickerSingle.Builder(getActivity())
                        .setData(cyl_)
                        .setDefaultIndex(8)
                        .setOnItemSelectListener(new AlertPickerSingle.OnItemSelectListener() {
                            @Override
                            public void endSelect(AlertPickerSingle chooser, boolean isCancel, int id, String text) {
                                rightSgImg.setVisibility(View.GONE);
                                rightSgTv.setText(text);
                            }
                        })
                        .show();
                break;
            case R.id.tjRv:
                ArrayList<String> tj = new ArrayList<>();
                for (int i = 44; i < 75; i++) {
                    tj.add(i + "");
                }
                new AlertPickerSingle.Builder(getActivity())
                        .setData(tj)
                        .setOnItemSelectListener(new AlertPickerSingle.OnItemSelectListener() {
                            @Override
                            public void endSelect(AlertPickerSingle chooser, boolean isCancel, int id, String text) {
                                tjImg.setVisibility(View.GONE);
                                tjTv.setText(text);
                            }
                        })
                        .show();
                break;
            case R.id.threeMonthLL:
                monthFlag = 3;
                setImgSelect(threeMonthImg, sixMonthImg, nineMonthImg);
                break;
            case R.id.sixMonthLL:
                monthFlag = 6;
                setImgSelect(sixMonthImg, threeMonthImg, nineMonthImg);
                break;
            case R.id.nineMonthLL:
                monthFlag = 9;
                setImgSelect(nineMonthImg, threeMonthImg, sixMonthImg);
                break;
            case R.id.sureTv:
                String leftEye = leftEyeTv.getText().toString();
                String rightEye = rightEyeTv.getText().toString();
                String leftSg = leftSgTv.getText().toString();
                String rightSg = rightSgTv.getText().toString();
                String mTj = tjTv.getText().toString();
                String leftAxl = leftAxlEv.getText().toString();
                String rightAxl = rightAxlEv.getText().toString();
                if (TextUtils.isEmpty(leftEye)) {
                    ToastUtil.shortShowToast("请先选择左眼度数...");
                } else if (TextUtils.isEmpty(rightEye)) {
                    ToastUtil.shortShowToast("请先选择右眼度数...");
                } else if (TextUtils.isEmpty(mTj)) {
                    ToastUtil.shortShowToast("请先选择瞳距...");
                } else {
                    if (!TextUtils.isEmpty(leftSg)) {
                        if (TextUtils.isEmpty(leftAxl)) {
                            ToastUtil.shortShowToast("请填写左眼轴位...");
                            return;
                        }
                    }
                    if (!TextUtils.isEmpty(rightSg)) {
                        if (TextUtils.isEmpty(rightAxl)) {
                            ToastUtil.shortShowToast("请填写右眼轴位...");
                            return;
                        }
                    }
                    if (!TextUtils.isEmpty(leftAxl)) {
                        if (TextUtils.isEmpty(leftSg)) {
                            ToastUtil.shortShowToast("请填写左眼散光...");
                            return;
                        }
                    }
                    if (!TextUtils.isEmpty(rightAxl)) {
                        if (TextUtils.isEmpty(rightSg)) {
                            ToastUtil.shortShowToast("请填写右眼散光...");
                            return;
                        }
                    }
                    CheckMenu checkMenu = new CheckMenu();
                    checkMenu.setLeftSPH(leftEye);
                    checkMenu.setRightSPH(rightEye);
                    checkMenu.setLeftCYL(leftSg);
                    checkMenu.setRightCYL(rightSg);
                    checkMenu.setEyesPD(mTj);
                    checkMenu.setLeftAXL(leftAxl);
                    checkMenu.setRightAXL(rightAxl);
                    checkMenu.setReCheck(monthFlag);
                    checkMenu.setUpdateTime(System.currentTimeMillis());
                    AppLog.e(checkMenu);
                    BussEvent event = new BussEvent(BussEvent.Save_Light_Order);
                    event.setMessage(checkMenu);
                    EventBus.getDefault().post(event);
                    MyApplication.getInstance().manager.onBackPressed();
                }
                break;
        }
    }

    private void setImgSelect(ImageView img1, ImageView img2, ImageView img3) {
        img1.setImageResource(R.mipmap.icon_xztb);
        img2.setImageResource(R.mipmap.icon_wxztb);
        img3.setImageResource(R.mipmap.icon_wxztb);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.inject(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
